/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 TCPSharedResources.cpp. Created on: 19.08.2009 12:53:00
 */

#include "smartobjects/network/tcp/TCPSharedResources.hpp"

namespace smartobjects {

const char* 		TCPSharedResources::DEFAULT_GROUP_NAME 				= "DEFAULT";
const unsigned int 	TCPSharedResources::DEFAULT_INIT_THREADS_CNT 		= 1;
const unsigned int 	TCPSharedResources::DEFAULT_MAX_THREADS_CNT 		= 5;
const char* 		TCPSharedResources::TCP_SHARED_THREAD_POOL_ID		= "TCP_SHARED_THREAD_POOL";
const char* 		TCPSharedResources::TCP_SHARED_HANDSHAKE_TIMER_ID	= "TCP_SHARED_HANDSHAKE_TIMER";


TCPSharedResources::TCPSharedResources()
		: m_poHandshakeManagerFactory ( NULL ),
		  m_poPacketFormerFactory ( NULL ),
		  m_poHandshakeTimer( new cpputils::ept::Timer(TCP_SHARED_HANDSHAKE_TIMER_ID) ),
		  m_oSequenceGenerator(1)  {
}

TCPSharedResources::~TCPSharedResources() {
	if( m_poPacketFormerFactory ) {
		m_poPacketFormerFactory->release();
		m_poPacketFormerFactory = NULL;
	}

	if( m_poHandshakeManagerFactory ) {
		m_poHandshakeManagerFactory->release();
		m_poHandshakeManagerFactory = NULL;
	}

	m_poHandshakeTimer.reset();
}

IPacketFormerFactory* TCPSharedResources::getPacketFormerFactory() {
	return m_poPacketFormerFactory;
}

void TCPSharedResources::setPacketFormerFactory(IPacketFormerFactory* poFactory) {
	if(m_poPacketFormerFactory)
		m_poPacketFormerFactory->release();
	m_poPacketFormerFactory = poFactory;
	m_poPacketFormerFactory->addRef();
}

IHandshakeManagerFactory* TCPSharedResources::getHandshakeManagerFactory() {
	return m_poHandshakeManagerFactory;
}

void TCPSharedResources::setHandshakeManagerFactory(IHandshakeManagerFactory* poFactory) {
	if(m_poHandshakeManagerFactory)
		m_poHandshakeManagerFactory->release();
	m_poHandshakeManagerFactory = poFactory;
	m_poHandshakeManagerFactory->addRef();
}

ThreadPool::SharedPtr TCPSharedResources::getThreadPool() {
	return getThreadPool ( DEFAULT_GROUP_NAME );
}

ThreadPool::SharedPtr TCPSharedResources::getThreadPool(const std::string& sGroupName) {
	boost::recursive_mutex::scoped_lock oScopedLock ( m_mxResources );
	ThreadPool::SharedPtr result;
	ThreadPools::iterator it = m_mThreadPools.find(sGroupName);
	if ( it !=  m_mThreadPools.end() ) {
		result = it->second;
	}
	else {
		result = ThreadPool::SharedPtr(
				new ThreadPool(DEFAULT_INIT_THREADS_CNT, DEFAULT_MAX_THREADS_CNT)
		);
		std::pair< std::string, ThreadPool::SharedPtr > oPair ( sGroupName, result );
		m_mThreadPools.insert(oPair);
	}
	return result;
}

smartobjects::sockets::SocketMultiplexer::SharedPtr TCPSharedResources::getSocketMultiplexer() {
	return getSocketMultiplexer ( DEFAULT_GROUP_NAME );
}

smartobjects::sockets::SocketMultiplexer::SharedPtr TCPSharedResources::getSocketMultiplexer(const std::string& sGroupName) {
	boost::recursive_mutex::scoped_lock oScopedLock ( m_mxResources );
	smartobjects::sockets::SocketMultiplexer::SharedPtr result;
	SocketMultiplexers::iterator it = m_mSocketMultiplexers.find(sGroupName);
	if ( it ==  m_mSocketMultiplexers.end() || (it!= m_mSocketMultiplexers.end() && it->second->isFull() ) ) {
		result = smartobjects::sockets::SocketMultiplexer::SharedPtr(
				new smartobjects::sockets::SocketMultiplexer()
		);
		std::pair< std::string, smartobjects::sockets::SocketMultiplexer::SharedPtr > oPair ( sGroupName, result );
		m_mSocketMultiplexers.insert(oPair);
	}
	else {
		result = it->second;
	}
	return result;
}

Timer::SharedPtr TCPSharedResources::getHandshakeTimer() {
	return m_poHandshakeTimer;
}

cpputils::utils::SequenceGenerator<long>& TCPSharedResources::getSequenceGenerator() {
	return m_oSequenceGenerator;
}

}
